<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <div style="font-size: 14px; font-weight: bold; margin-bottom: 30px;color: #606266;">
        文章状态：
        <el-radio v-model="newsStatus" label="" @change="queryListByStatus" size="mini">全部</el-radio>
        <el-radio v-model="newsStatus" label="0" @change="queryListByStatus" size="mini">草稿</el-radio>
        <el-radio v-model="newsStatus" label="1" @change="queryListByStatus" size="mini">待审核</el-radio>
        <el-radio v-model="newsStatus" label="4" @change="queryListByStatus" size="mini">审核通过</el-radio>
        <el-radio v-model="newsStatus" label="9" @change="queryListByStatus" size="mini">已发布</el-radio>
        <el-radio v-model="newsStatus" label="2" @change="queryListByStatus" size="mini">审核失败</el-radio>
      </div>


      <div style="font-size: 14px; font-weight: bold; color: #606266;">
        频道列表：
        <el-select v-model="newsChannel" size="mini" placeholder="全部" @change="getListByChannel()">
          <el-option key="" label="全部" value=""></el-option>
          <el-option
            v-for="item in cList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>

        <span style="margin-left: 20px;">发布日期：</span>
        <el-date-picker
          v-model="newsCreateDate"
          size="mini"
          @change="queryByNewsCreateDate"
          type="daterange"
          range-separator="-"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>


      </div>
<!--
      <el-form-item label="发布时间" prop="publishTime">
        <el-date-picker
          clearable
          v-model="queryParams.publishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="发布时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-button @click="aliTest">文本审核测试</el-button>-->
    </el-form>
    <el-divider></el-divider>
    <div>
      <ul class="ulstyle news_table clearfix_news" >

        <li v-for="(m,index) in newsList" :key="index">
          <div class="news_btn">
            <el-button v-if="m.enable==0" icon="el-icon-upload2" circle @click="setDownOrUp(m.id,1)"></el-button>
            <el-button v-else  icon="el-icon-download" circle @click="setDownOrUp(m.id,0)"></el-button>
            <router-link :to="{name:'CreateNews', params:{'id':m.id}}">
              <el-button icon="el-icon-edit" circle  ></el-button>
            </router-link>
            <el-button icon="el-icon-delete" circle @click="handleDelete(m.id)"></el-button>
          </div>
          <el-image
            style="width: 199px; height: 160px;border-radius: 10px; border-bottom-left-radius: 0;  border-bottom-right-radius: 0;"
            :src="getImage(m.images)"></el-image>
          <div style=" width:200px;">
            <div style="font-size: 13px; text-indent: 1em; font-weight: bold; padding: 10px; height: 60px;">{{m.title}}</div>
            <div style="width:100%;display: flex;justify-content:space-between;flex-direction: row; align-items: center;font-size: 12px; padding-left: 10px;padding-right: 10px;padding-top: 30px; ">
              <div>{{getCreateTime(m.createTime)}}</div>

              <el-tag :type="getType(m.status)">{{getStatus(m.status)}}</el-tag>
            </div>
          </div>
        </li>

      </ul>

    </div>


    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="图文内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="标签" prop="labels">
          <el-input v-model="form.labels" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="提交时间" prop="submitedTime">
          <el-date-picker
            clearable
            v-model="form.submitedTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择提交时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="定时发布时间，不定时则为空" prop="publishTime">
          <el-date-picker
            clearable
            v-model="form.publishTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择定时发布时间，不定时则为空"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="拒绝理由" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入拒绝理由" />
        </el-form-item>
        <el-form-item label="图片" prop="images">
          <el-input
            v-model="form.images"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listNews,
  getNews,
  delNews,
  addNews,
  updateNews,
  ali_text,
  down_or_up
} from "@/api/wemedia/news";
import {channelList4News} from "@/api/wemedia/channel";

export default {
  name: "News",
  data() {
    return {
      newsCreateDate: [],
      newsChannel:"",
      cList: [],
      newsStatus:"",
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章列表表格数据
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        title: undefined,
        content: undefined,
        type: undefined,
        channelId: undefined,
        labels: undefined,
        submitedTime: undefined,
        status: undefined,
        publishTime: undefined,
        reason: undefined,
        articleId: undefined,
        images: undefined,
        enable: undefined,
        beginPubDate: undefined,
        endPubDate: undefined

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
    };
  },
  created() {
    this.getList();
    this.getChannelList();
  },
  activated(){
    this.getList();
    this.getChannelList();
  },
  methods: {
    setDownOrUp(id,enable){
      down_or_up({"id": id,"enable":enable})
        .then((response) => {
          console.log(response);
          this.$modal.msgSuccess("操作成功！");
          this.getList();
        });
    },
    aliTest(){
      ali_text();
    },
    queryByNewsCreateDate(){
      if(this.newsCreateDate && this.newsCreateDate.length > 0) {
        this.queryParams.beginPubDate = this.newsCreateDate[0];
        this.queryParams.endPubDate = this.newsCreateDate[1];

      }else{
        this.queryParams.beginPubDate = undefined;
        this.queryParams.endPubDate = undefined;
      }
      this.getList();
    },
    getListByChannel(){
      this.queryParams.channelId = this.newsChannel;
      this.getList();
    },
    getChannelList(){
      channelList4News().then((response) => {
        this.cList = response.data;
      });
    },
    queryListByStatus(){
      this.queryParams.status = this.newsStatus;
      this.getList();

    },
    getType(status){

      if(status == "0") return "info";
      if(status == "1") return "";
      if(status == "2") return "danger";
      if(status == "3") return "warning";
      if(status == "4") return "success";
      if(status == "8") return "";
      if(status == "9") return "success";
    },
    getStatus(status){
      if(status == "0") return "草稿";
      if(status == "1") return "待审核";
      if(status == "2") return "审核失败";
      if(status == "3") return "人工审核";
      if(status == "4") return "人工审核通过";
      if(status == "8") return "待发布";
      if(status == "9") return "已发布";
    },
    getCreateTime(date){
      return date.substring(0,date.indexOf(" "));
    },
    getImage(imgs){
      if([null,undefined,NaN,false].includes(imgs))return "";
      let i = imgs.indexOf(",");
      return i == -1 ? imgs : imgs.substring(0,i);
    },
    /** 查询文章列表列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams).then((response) => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        title: undefined,
        content: undefined,
        type: undefined,
        channelId: undefined,
        labels: undefined,
        createTime: undefined,
        submitedTime: undefined,
        status: "0",
        publishTime: undefined,
        reason: undefined,
        articleId: undefined,
        images: undefined,
        enable: undefined,
        createBy: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getNews(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改文章列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateNews(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addNews(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$modal
        .confirm('是否确认删除文章列表编号为"' + id + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delNews(id);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wemedia/news/export",
        {
          ...this.queryParams,
        },
        `news_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style>
.news_btn{
  position:absolute;z-index:999; padding-left: 80px; padding-top:10px;opacity:0%;
}
.news_btn:hover{
  opacity:100%;
}
.ulstyle {

  list-style: none;
}
.clearfix_news::after {
  content: '';
  display: table;
  clear: both;
}
.news_table {
  width: 100%;
}
.news_table li {
  float: left;
  width: 200px;
  height: 300px;
  /*margin-top: -1px;
  margin-left: -1px;
  border: 1px solid #000;*/
  border: 1px solid #bbbbbb;

  margin-right: 20px;
  margin-bottom: 20px;
  border-radius:10px;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  box-sizing: border-box;
}

</style>
